package com.longtop.perform.zb.team.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.longtop.framework.base.BaseService;
import com.longtop.framework.common.Constant;
import com.longtop.framework.common.QueryConditions;
import com.longtop.framework.common.QueryResult;
import com.longtop.framework.security.BaseCheckedException;
import com.longtop.framework.security.SessionObject;
import com.longtop.framework.util.StringUtil;
import com.longtop.intelliweb.view.Record;
import com.longtop.perform.zb.team.model.TZbTeam;
import com.longtop.perform.zb.team.model.TZbTeamMx;

public class TeamService extends BaseService {

	public static final String BEAN_ID = "teamService";

	/**
	 * 依据条件，读取代码类别列表
	 * 
	 * @param queryConditions
	 *            查询条件
	 * @param pageNo
	 *            查询结果的起始页码。
	 * @param pageSize
	 *            查询结果的最大行数。如果不需要，则设置为-1
	 * @return
	 */
	public QueryResult loadTeam(QueryConditions queryConditions, int pageNo, int pageSize) {

		String hql = "from TZbTeam";
		String[] propertyNames = null;
		String[] operators = null;
		Object[] values = null;

		String orderBy = "";

		if (queryConditions != null) {
			propertyNames = StringUtil.listToStrArray(queryConditions.getPropertyNames());
			operators = StringUtil.listToStrArray(queryConditions.getOperators());
			List valuesList = queryConditions.getValues();
			values = valuesList == null || valuesList.size() == 0 ? null : valuesList.toArray();
		}

		int offset = (pageNo - 1) * pageSize;

		// 执行查询
		Object[] result = this.query(hql, propertyNames, operators, values, offset, pageSize, true, orderBy, null, null);

		return new QueryResult((List) result[0], result[1] != null ? ((Integer) result[1]).intValue() : 0);
	}

	/**
	 * 保存、修改一起做！
	 * 
	 * @param changeMap
	 *            对象列表，有三个值，删除主键列表、更新bean的列表、新增bean的列表
	 * @param sessionObject
	 * @return 操作成功受影响的记录数，有三个值，插入成功的记录数、更新成功的记录数、删除成功的记录数
	 * @throws BaseCheckedException
	 */
	public Integer[] saveTeam(Map changeMap, SessionObject sessionObject) throws BaseCheckedException {

		// 插入成功的记录数、更新成功的记录数、删除成功的记录数
		// 插入成功的记录数、更新成功的记录数、删除成功的记录数
		int insertCount = 0;
		int updateCount = 0;
		int deleteCount = 0;

		// 删除主键列表、更新bean的列表、新增bean的列表
		List insertBeanList = (ArrayList) changeMap.get(Constant.INSERT_BEAN_LIST);
		List updateBeanList = (ArrayList) changeMap.get(Constant.UPDATE_BEAN_LIST);
		String idList = (String) changeMap.get(Constant.ID_LIST);
		sessionObject.getUserName();
		TZbTeam zbTeam;
		Iterator iterator;

		// 批量插入记录
		if (insertBeanList.size() > 0) {
			iterator = insertBeanList.iterator();
			while (iterator.hasNext()) {
				zbTeam = (TZbTeam) iterator.next();
				
				this.setAuthPropetryToEntity(zbTeam, sessionObject, true);
				
				// 校验指标集合名称是否重复。
				List lists = this.queryAll("from TZbTeam where teamName='" + zbTeam.getTeamName() + "'");
				if (lists.size() > 0)
					throw new BaseCheckedException("", "保存失败,指标集合名称【" + zbTeam.getTeamName() + "】已经存在!");
				super.save(zbTeam);
				insertCount++;
			}
		}

		// 批量更新记录
		if (updateBeanList.size() > 0) {
			iterator = updateBeanList.iterator();
			while (iterator.hasNext()) {
				zbTeam = (TZbTeam) iterator.next();
				this.setAuthPropetryToEntity(zbTeam, sessionObject, false);
				super.update(zbTeam);
				// 删除关联
				updateCount++;
			}
		}

		// 批量删除记录
		if (idList.length() > 0) {
			String[] teamIds = idList.split(",");
			for (int i = 0; i < teamIds.length; i++) {
				String teamId = teamIds[i];
				deleteTeam(teamId);
				deleteCount++;
			}
		}
		return new Integer[] { new Integer(insertCount), new Integer(updateCount), new Integer(deleteCount) };
	}

	/**
	 * 删除指标集合
	 * 
	 * @param userId
	 * @throws BaseCheckedException
	 */
	public void deleteTeam(String teamId) {
		// 保存
		String sql = "delete from TZbTeamMx where teamId ='" + teamId + "'";
		super.batchUpdate(sql);
		super.delete(TZbTeam.class, teamId);
	}

	/**
	 * 更新关联的指标
	 * 
	 * @param teamId
	 *          更新的指标集合ID
	 * @param teamIndexList
	 * 			更新的关联指标
	 * 
	 * @return
	 */
	public void updateRelation(String teamId,List teamIndexList) throws BaseCheckedException {
		String sql = "delete from TZbTeamMx where teamId ='" + teamId + "'";
		super.batchUpdate(sql);
		for (int i = 0; i < teamIndexList.size(); i++) {
			TZbTeamMx zbTeamIndex = new TZbTeamMx();
			Record record = (Record) teamIndexList.get(i);
			if (record.getState() == 3) {
			} else {
				String indexId = record.getValue("indexId").toString();
				zbTeamIndex.setIndexId(indexId);
				zbTeamIndex.setTeamId(teamId);
				super.save(zbTeamIndex);
			}
		}
	}
	
	/**
	 * 删除指标时删除指标集合中相关指标
	 * 
	 * @param menuId
	 * @return
	 */
	public void deleteTeamByIndexId(String indexId) {
		String sql = "delete from TZbTeamMx where indexId =" + indexId+"" ;
		super.batchUpdate(sql);
	}
}